package com.caine.androidtask.ls02;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.caine.androidtask.R;

public class Ls02 extends Activity {
	private int currentColor = 0;
	final int[] colors = new int[] { R.color.color1, R.color.color2,
			R.color.color3, R.color.color4, R.color.color5, R.color.color6,
			R.color.color7 };

	final int[] names = new int[] { R.id.view01, R.id.view02, R.id.view03,
			R.id.view04, R.id.view05, R.id.view06, R.id.view07 };

	TextView[] views = new TextView[7];

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.caine_ls02);		
		init();
	}

	void init() {
		for (int i = 0; i < 7; i++) {
			views[i] = (TextView) findViewById(names[i]);
		}
		// =====================
		final Handler handler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				if (msg.what == 0X0001) {
					for (int i = 0; i < 7 - currentColor; i++) {
						views[i].setBackgroundColor(getResources().getColor(
								colors[i + currentColor]));
					}

					for (int i = 7 - currentColor, j = 0; i < 7; i++, j++) {
						views[i].setBackgroundColor(getResources().getColor(
								colors[j]));
					}
					super.handleMessage(msg);
				}
			};
		};
		// =====================

		new Timer().schedule(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				currentColor++;
				if (currentColor >= 6) {
					currentColor = 0;
				}
				Message msg = new Message();
				msg.what = 0X0001;
				handler.sendMessage(msg);
			}
		}, 0, 100);

	}

}
